/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.museum.main;

import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.museum.utils.Utils;

public class DigBatch {
	String[] users = new String[] { "chinesegxf" };
	String[] uids = new String[] { "4209691217" };
	String[] cookies = new String[] { "MY:SESSION=4e8025b303teliiha73008eb2284e0b3; MY:my_sig_prefix=http%3A%2F%2Fwww.btbird.com%2F; city_id=0; city_old_id=0; MY:UserID=4209691217; MY:SNID=9471d911b04f701f4f3f19151e814da6", };
	int delay = 2000 / users.length;
	boolean debug = true;
	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	DefaultHttpClient hc = new DefaultHttpClient();

	public void dig() throws Exception {
		HttpPost post = Utils.createPost();
		while (true) {
			for (int j = 0; j < uids.length; j++) {
				try {
					Thread.sleep(delay);
					String url = "http://my.mowhere.com/interface/manage_treasure.php";
					post.setURI(new URI(url));
					post.setHeader("Cookie", cookies[j]);
					post.setEntity(Utils.genDigBatchOkEntity(uids[j]));
					String resjson = Utils.trans(hc.execute(post));
					JsonObject js = new JsonParser().parse(resjson)
							.getAsJsonObject();
					debug(j, js);

					String err = js.get("err").getAsString();
					info(j, err);
					sell(post, j);
					buy(post, j);
					use(post, j);

					Thread.sleep(2000);
					post.setEntity(Utils.genDigBatchEntity(uids[j]));
					resjson = Utils.trans(hc.execute(post));
					js = new JsonParser().parse(resjson).getAsJsonObject();
					debug(j, js);

					err = js.get("err").getAsString();
					info(j, err);

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			Thread.sleep(60 * 60 * 1000);
		}
	}

	public void buy(HttpPost post, int uidx) throws Exception {
		String url = "http://my.mowhere.com/interface/manage_treasure.php";
		post.setURI(new URI(url));
		post.setHeader("Cookie", cookies[uidx]);
		post.setEntity(Utils.genBuyEntity(uids[uidx], "402"));
		String err = "1";
		while (!"".equals(err)) {
			Thread.sleep(2000);
			String resjson = Utils.trans(hc.execute(post));
			JsonObject js = new JsonParser().parse(resjson).getAsJsonObject();
			err = js.get("err").getAsString();
			JsonObject data = js.getAsJsonObject("data");
			String r = data.get("text").getAsString();
			info(uidx, r);
		}
	}

	public void use(HttpPost post, int uidx) throws Exception {
		String url = "http://my.mowhere.com/interface/manage_treasure.php";
		post.setURI(new URI(url));
		post.setHeader("Cookie", cookies[uidx]);
		post.setEntity(Utils.genUseEntity(uids[uidx], "402"));
		String err = "1";
		while (!"".equals(err)) {
			Thread.sleep(2000);
			String resjson = Utils.trans(hc.execute(post));
			JsonObject js = new JsonParser().parse(resjson).getAsJsonObject();
			err = js.get("err").getAsString();
			JsonObject data = js.getAsJsonObject("data");
			String r = data.get("text").getAsString();
			info(uidx, r);
		}
	}

	public void sell(HttpPost post, int uidx) throws Exception {
		String url = "http://my.mowhere.com/interface/manage_treasure.php";
		post.setURI(new URI(url));
		post.setHeader("Cookie", cookies[uidx]);
		post.setEntity(Utils.genSellEntity(uids[uidx], ""));
		String err = "1";
		while (!"".equals(err)) {
			Thread.sleep(2000);
			String resjson = Utils.trans(hc.execute(post));
			JsonObject js = new JsonParser().parse(resjson).getAsJsonObject();
			err = js.get("err").getAsString();
			JsonObject data = js.getAsJsonObject("data");
			String r = data.get("text").getAsString();
			info(uidx, r);
		}
	}

	private void debug(int i, Object s) {
		if (debug)
			System.out.println(users[i] + " # " + df.format(new Date())
					+ " debug: " + s);
	}

	private void info(int i, Object s) {
		System.out.println(users[i] + " # " + df.format(new Date()) + " info: "
				+ s);
	}

	public static void main(String[] args) throws Exception {
		DigBatch r = new DigBatch();
		r.dig();
	}

}
